package com.yowasa.xiuxian.engine.message.battle;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.yowasa.xiuxian.annoations.CMD;
import com.yowasa.xiuxian.engine.BattleEngine;
import com.yowasa.xiuxian.engine.message.CmdMessageHandler;
import com.yowasa.xiuxian.entity.bo.battle.BattleContent;
import com.yowasa.xiuxian.entity.bo.user.UserBO;
import com.yowasa.xiuxian.entity.mys.SendMessage;
import com.yowasa.xiuxian.entity.po.XiuXianExtend;
import com.yowasa.xiuxian.service.helper.BattleHelper;
import com.yowasa.xiuxian.service.helper.ExtendHelper;
import com.yowasa.xiuxian.service.helper.MessageHelper;
import com.yowasa.xiuxian.service.helper.UserHelper;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.stream.Collectors;


@CMD(name="/深境螺旋")
public class AbyssHandler extends CmdMessageHandler {
    @Autowired
    private MessageHelper messageHelper;
    @Autowired
    private UserHelper userHelper;
    @Autowired
    private BattleEngine battleEngine;
    @Autowired
    private BattleHelper battleHelper;
    @Autowired
    private ExtendHelper extendHelper;


    @Override
    public void execute(SendMessage message) {
        UserBO firstUser = userHelper.checkOpUserAll(message);

        List<String> params = getAllParam(message);//获取名称
        if (StringUtils.isEmpty(params.get(0))) {
            messageHelper.replyMys(message, "可以在后面追加 查看 挑战 等二级指令进行后续操作");
            return;
        }
        //获取当前层数
        XiuXianExtend byAccountId = extendHelper.getByAccountId(firstUser.getId());
        Integer i = byAccountId.getAbyss() == 0 ? 1001 : 1000+byAccountId.getAbyss()+1;
        UserBO secondUser = userHelper.getByUserId(i+"");
        if ("查看".equals(params.get(0))) {
            StringBuffer sb = new StringBuffer();
            sb.append("你当前处于深境螺旋第").append(i-1000).append("层\n")
                    .append("本期buff为：元素精通\n")
                    .append("效果：按灵根浓度提升你的战斗属性\n")
                    .append("金：物攻 火：法攻 水：法抗 土：物抗 木：生命\n")
                    .append("\n本层怪物为：").append(secondUser.getName())
                    .append("\n怪物信息：\n").append(secondUser.getUserBattle().getDesc())
                    .append("\n怪物战技：\n").append(String.join("\n", secondUser.getUserConfig().getBattleStrategyList()));
            messageHelper.replyMys(message, sb.toString());

        }
        if ("挑战".equals(params.get(0))) {

            BattleContent battle = battleEngine.abyssBattle(firstUser, secondUser);

            battleHelper.syncUserState(battle);
            StringBuffer msg = new StringBuffer();
            if (battle.getResult() == 1) {
                byAccountId.setAbyss(byAccountId.getAbyss() + 1);
                byAccountId.setRough(byAccountId.getRough() + 150);
                extendHelper.updateExtendById(byAccountId);
                msg.append("你通过了本层深渊，获得原石150\n");
            }
            int days = battleHelper.calCostTime(battle);
            userHelper.lockUser(battle.getFirst().getUserBO(),days,"正在挑战深境螺旋");
            userHelper.saveUserInfo(battle.getFirst().getUserBO());
            msg.append(battle.getMsgLog().stream().collect(Collectors.joining("\n")));
            messageHelper.replyMys(message, msg.toString());
        }


    }

    public String desc(){
        return "深渊挑战";
    }


}
